import {
  imageUrl
} from "../../utils/imageutil"
let app = getApp()
Page({

  data: {
    imageUrl,
    nav: [{
      title: '服务',
      type: 1,
    }, {
      title: '商家',
      type: 2,
    }],
    nav_curr: 1,

    name: '',

    housekeeperListPage: 1,
    housekeeperListFlag: true,
    housekeeperList: [],

    store_flag: true,
    store_list: [],
    store_page: 1

  },


  onLoad(options) {
    this.gethousekeeperList()
  },
  onPullDownRefresh() {
    this.setData({
      nav_curr: 1,
      name: '',
      housekeeperListPage: 1,
      housekeeperListFlag: true,
      housekeeperList: [],

      store_flag: true,
      store_list: [],
      store_page: 1
    })
    this.gethousekeeperList()
  },
  onReachBottom() {
    if (this.data.nav_curr == 1) {
      this.gethousekeeperList()
    } else {
      this.get_storeList()
    }
  },

  change_name(e){
    this.setData({
      name:e.detail.value,
      housekeeperListPage: 1,
      housekeeperListFlag: true,
      housekeeperList: [],

      store_flag: true,
      store_list: [],
      store_page: 1
    })
    if(this.data.nav_curr==1){
      this.gethousekeeperList()
    }else{
      this.get_storeList()
    }
  },

  gethousekeeperList() {
    if (!this.data.housekeeperListFlag) {
      return
    }
    this.setData({
      housekeeperListFlag: false
    })
    app.globalData.post({
      data: {
        do: 'housekeeperList',
        uid:wx.getStorageSync('userInfo').id,
        page: this.data.housekeeperListPage,
        pagesize: 10,
        name: this.data.name
      }
    }).then(res => {
      wx.stopPullDownRefresh();
      if (res.data.data == '') {
        this.setData({
          housekeeperListFlag: false
        })
      } else {
        let arr = this.data.housekeeperList.concat(res.data.data);
        this.setData({
          housekeeperListFlag: true,
          housekeeperList: arr,
          housekeeperListPage: this.data.housekeeperListPage + 1
        })
      }
    })
  },

  get_storeList() {
    if (!this.data.store_flag) {
      return
    }
    this.setData({
      store_flag: false
    })
    app.globalData.post({
      data: {
        do: 'StoreList',
        page: this.data.store_page,
        pagesize: 10,
        keywords: this.data.name,
      }
    }).then(res => {
      wx.stopPullDownRefresh();
      if (res.data.length == 0) {
        this.setData({
          store_flag: false
        })
      } else {
        let arr = this.data.store_list.concat(res.data);
        this.setData({
          store_flag: true,
          store_list: arr,
          store_page: this.data.store_page + 1
        })
      }
    })
  },

  change_nav_curr(e) {
    this.setData({
      nav_curr: e.currentTarget.dataset.type,

      housekeeperListPage: 1,
      housekeeperListFlag: true,
      housekeeperList: [],

      store_flag: true,
      store_list: [],
      store_page: 1
    })
    if(this.data.nav_curr==1){
      this.gethousekeeperList()
    }else{
      this.get_storeList()
    }
  },

  to_aunt(e){
    wx.navigateTo({
      url: '../aunt/index?uid='+e.currentTarget.dataset.id+'&type=' + this.data.titlenavcurr,
    })
  },

  to_StoreInfo(e){
    wx.navigateTo({
      url: '../housekeeping/index?store_id='+ e.currentTarget.dataset.id,
    })
  },

})